package com.mzxy.boxuegu.activity;

import android.support.v4.text.TextUtilsCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.boxuegu.R;
import com.mzxy.boxuegu.utils.MD5Utils;
import com.mzxy.boxuegu.utils.SpUtils;

public class ModifyPswActivity extends AppCompatActivity {
    private TextView tvMainTitle;
    private TextView tvBack;

    private EditText mEtOriginalPsw;
    private EditText mEtNewPwd;
    private EditText mEtNewPwdAgain;
    private Button mBtnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_psw);
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        tvMainTitle.setText("修改密码");
        tvBack = (TextView) findViewById(R.id.tv_back);

        mEtNewPwd = (EditText) findViewById(R.id.et_new_pwd);
        mBtnSave = (Button) findViewById(R.id.btn_save);
        mEtNewPwdAgain = (EditText) findViewById(R.id.et_new_pwd_again);
        mEtOriginalPsw = (EditText) findViewById(R.id.et_original_psw);


        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String originalPsw = mEtOriginalPsw.getText().toString();
                String newPsw = mEtNewPwd.getText().toString();
                String newPswAgain = mEtNewPwdAgain.getText().toString();
                if(TextUtils.isEmpty(originalPsw) || TextUtils.isEmpty(newPsw) || TextUtils.isEmpty(newPswAgain)){
                    Toast.makeText(ModifyPswActivity.this,"原始密码与新密码均不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                //1. 比较sp里面保存的密码和输入的原始密码是否一致
                String oldPsw = SpUtils.getPsw(ModifyPswActivity.this,SpUtils.getLoginUsername(ModifyPswActivity.this));
                if(!MD5Utils.md5(originalPsw).equals(oldPsw)){
                    Toast.makeText(ModifyPswActivity.this,"原始密码不正确",Toast.LENGTH_SHORT).show();
                    return;
                }
                //2. 新密码不能与原始密码一致
                if (originalPsw.equals(newPsw)) {
                    Toast.makeText(ModifyPswActivity.this,"新密码不能与原始密码一致",Toast.LENGTH_SHORT).show();
                    return;
                }
                //3. 两次输入的新密码必须相同
                if (!newPswAgain.equals(newPsw)) {
                    Toast.makeText(ModifyPswActivity.this,"两次输入的新密码必须相同",Toast.LENGTH_SHORT).show();
                    return;
                }

                //4. 保存到sp里面，替换当前user的密码
                SpUtils.saveRegisterInfo(ModifyPswActivity.this,SpUtils.getLoginUsername(ModifyPswActivity.this),newPsw);
                Toast.makeText(ModifyPswActivity.this,"密码修改成功",Toast.LENGTH_SHORT).show();
                finish();
            }
        });

        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
